using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrackSystem.IO.Data;
using TrackSystem.IO.Models;

namespace TrackSystem.IO.Protocol
{
    /// <summary>
    /// 绞车A架状态数据
    /// 0 -- 导接头接近开关及锁紧信号 0表示接近开关打开和导接头锁紧关闭，1表示接近开关关闭，2-表示导接头锁紧开关关闭，3-表示接近开关关闭
    /// 1 -- 缆速（m/min）
    /// 2 -- 拉力（T）
    /// 3 -- 缆长（m）
    /// </summary>
    public class WinchProtocol : ProtocolBase
    {
        private int startTicks;
        private bool bSendR;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public WinchProtocol(string name)
            : base()
        {
            NeedInit = false;
            Name = name;
            bSendR = false;
        }

        /// <summary>
        /// 生成读取寄存器的通讯帧
        /// </summary>
        /// <returns></returns>
        public override byte[] GenerateReadFrame(out int NeedRecvLen)
        {
            startTicks = Environment.TickCount;
            if (bSendR)
            {
                Fail_R++;
                // 失败超过10次，成功次数清零
                if (Fail_R >= 10)
                {
                    Succ_R = 0;
                }
            }
            bSendR = true;
            NeedRecvLen = 40;
            return null;
        }


        /// <summary>
        /// 根据收到的内容进行处理
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public override bool ProcessReadFrame(byte[] buffer)
        {
            Rx_r = buffer;
            Lapse = Environment.TickCount - startTicks;
            var DataFactory = RegFactory.Instance;

            try
            {
                Rx_r = buffer;
                Lapse = Environment.TickCount - startTicks;
                if (buffer.Length !=15 || buffer[0] != 0xAA || buffer[14] != 0xBB)
                {
                    return false;
                }
                try {
                    double value1 = buffer[1]; // 导接头信号
                    var value2 = new Byte[4] { buffer[5], buffer[4], buffer[3], buffer[2] };
                    var value3 = new Byte[4] { buffer[9], buffer[8], buffer[7], buffer[6] };
                    var value4 = new Byte[4] { buffer[13], buffer[12], buffer[11], buffer[10] };

                    DataFactory.SetVal(RegNameList[0], value1);
                    DataFactory.SetVal(RegNameList[1], BitConverter.ToSingle(value2, 0));
                    DataFactory.SetVal(RegNameList[2], BitConverter.ToSingle(value3, 0));
                    DataFactory.SetVal(RegNameList[3], BitConverter.ToSingle(value4, 0));
                    Succ_R++;
                    bSendR = false;     // 正确收到，标志位变为false
                    Fail_R = 0;         // 通信成功，则将失败次数清零                 
                }
                catch (Exception)
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
